// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 * Creator: yanking
 * Create time: 2022-01-18 10:29
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.StreamTrain.transaction;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

public class runTrader {
    @Test
    public void shout() {
        // sort the transaction's date by stream way!
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader jim = new Trader("jim", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");
        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950),
                new Transaction(jim, 2022, 2950)
        );


        // return a string of all trader name sorted by alphabetically
//        String res = transactions.stream().map(t -> t.getTrader().getName()).sorted().reduce("", (n1, n2) -> n1 + n2);
//        System.out.println(res);


        //  Find the transaction with the smallest value.
//        System.out.println(transactions.stream().min(Comparator.comparing(Transaction::getValue)).get());
//        System.out.println(transactions.stream().sorted(Comparator.comparing(Transaction::getValue)).findFirst().get());


        //  What’s the highest value of all the transactions?
//        System.out.println(transactions.stream().map(Transaction::getValue).reduce(Math::max).get());


        //  Print all transactions’ values from the traders living in Cambridge.
//        transactions.stream().filter(a -> a.getTrader().getCity().equals("Cambridge")).map(
//                Transaction::getValue).forEach(k -> System.out.print(k + "\t"));


        // Use stream to check if there has trader base in millan
        //  transactions.stream().map(t -> t.getTrader().getCity().equals("Milan")).findAny().ifPresent(System.out::println);
//        transactions.stream().map(Transaction::getTrader).distinct().filter(
//                a -> a.getCity().equals("Milan")).forEach(System.out::println);
        System.out.println(transactions.stream().anyMatch(t -> t.getTrader().getCity().equals("Milan")));


//        transactions.stream().map(Transaction::getTrader).filter(
//                a -> a.getCity().equals("Milan")).findAny().ifPresent(d -> System.out.println(d.getName()));


//        transactions.stream().sorted(
//                Comparator.comparing(Transaction::getValue)).forEach(System.out::println);
        // To get the unique city that trader has been in
//        transactions.stream().map(Transaction::getTrader).map(
//                Trader::getCity).distinct().forEach(System.out::println);
        //  Find all traders from Cambridge and sort them by name.
//        transactions.stream().map(Transaction::getTrader).filter(
//                a -> a.getCity().equals("Cambridge")).sorted(Comparator.comparing(Trader::getName)).forEach(
//                System.out::println);
//        transactions.stream().map(Transaction::getTrader).filter(
//                a -> a.getCity().equals("Cambridge")).sorted(Comparator.comparing(Trader::getName)).forEach(
//                System.out::println);
        // Return a string of all traders names sorted alphabetically.
      /*  Stream<String> res = transactions.stream().map(Transaction::getTrader).map(Trader::getName).distinct();
        res.sorted(String::compareTo).forEach(System.out::println);*/


//        transactions.stream().map(Transaction::getTrader).map(
//                Trader::getName).distinct().sorted(String::compareTo).forEach(System.out::println);
//        transactions.stream().map(Transaction::getTrader).map(
//                Trader::getName).distinct().sorted(Comparator.comparing(Trader::getName)).forEach(System.out::println);


    }

    @Test
    public void shout02() {
        List<Integer> res = Arrays.asList(1, 2, 3, 6, 5, 7, 8, 99);
        System.out.println(res.stream().mapToInt(a -> a).max().orElse(1));
    }
}
